<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <!--<link rel="stylesheet" href="../plugins/elementui/index.css">-->
    <!-- 引入样式 网址引入-->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名"  v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @input="pagination.currentPage=1; findPage()"></el-input>

                <el-button @click="pagination.currentPage=1; findPage()" class="dalfBut">查询</el-button>

                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <template>
                <div>
                    <el-table
                            :data="tableData"
                            style="width: 100%"
                            row-key="id"
                            border
                            lazy
                            :tree-props="{children: 'children', hasChildren: 'level==1'}">

                        <el-table-column
                                prop="name"
                                label="菜单名称"
                                align="center">

                        </el-table-column>
                        <el-table-column
                                prop="linkUrl"
                                label="访问路径"
                                align="center">
                        </el-table-column>
                        <el-table-column
                                prop="path"
                                label="路径级别"
                                align="center">
                        </el-table-column>
                        <el-table-column
                                prop="parentMenuId"
                                label="自关联Id"
                                align="center">
                        </el-table-column>
                        <el-table-column
                                prop="level"
                                label="菜单级别"
                                sortable
                                align="center">
                        </el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <div class="pagination-container">
                        <el-pagination
                                class="pagiantion"
                                @current-change="handleCurrentChange"
                                :current-page="pagination.currentPage"
                                :page-size="pagination.pageSize"
                                layout="total, prev, pager, next, jumper"
                                :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" label-position="right" label-width="100px">
                                <el-row>

                                    <el-col :span="12">
                                        <el-form-item label="菜单名称">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="访问路径">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="路径级别">
                                            <el-input v-model="formData.path" ></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="优先级">
                                            <el-input v-model="formData.priority" ></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="父菜单">
                                            <el-select v-model="formData.parentMenuId" placeholder="请选择父菜单">
                                                <el-option
                                                        v-for="item in tableData"
                                                        :key="item.id"
                                                        :label="item.name"
                                                        :value="item.id">
                                                </el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="菜单级别">
                                            <el-input v-model="formData.level" ></el-input>
                                        </el-form-item>
                                    </el-col>

                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="图标">
                                            <el-input v-model="formData.icon" ></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="备注">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>


                    <!-- 新增编辑弹层 -->
                    <div class="Edit-form">
                        <el-dialog title="新增菜单" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" label-position="right" label-width="100px">
                                <el-row>

                                    <el-col :span="12">
                                        <el-form-item label="菜单名称">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="访问路径">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="路径级别">
                                            <el-input v-model="formData.path" ></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="优先级">
                                            <el-input v-model="formData.priority" ></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="父菜单">
                                            <el-select v-model="formData.parentMenuId" placeholder="请选择父菜单">
                                                <el-option
                                                        v-for="item in tableData"
                                                        :key="item.id"
                                                        :label="item.name"
                                                        :value="item.id">
                                                </el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="菜单级别">
                                            <el-input v-model="formData.level" ></el-input>
                                        </el-form-item>
                                    </el-col>

                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="图标">
                                            <el-input v-model="formData.icon" ></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="备注">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </template>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<!--<script src="../plugins/elementui/index.js"></script>-->
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            tableData: [],
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            formData:{},
            menuData:[],
            parentData:[],
            lableCheckedList:[],

        }, created() {
            this.findPage();//VUE对象初始化完成后调用分页查询方法，完成分页查询
        },
        methods: {


            findPage() {
                // var url = "/menu/findPage";

                axios.post("/menu/findPage.do",this.pagination).then(resp => {

                    if (resp.data.flag) {
                        this.tableData = resp.data.data.rows;
                        this.pagination.total = resp.data.data.total;
                        return;
                    }
                    this.$message.error(resp.data.message);
                }).catch(error=> {
                    this.showMessage(error)
                });

            },
            showMessage(r){
                console.log(r)
                if(r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },


            //编辑
            handleEdit() {
                //提交数据之前进行表单校验
                this.$refs['dataEditForm'].validate(valid => {
                    if (valid) {
                        //表单校验通过 , 发送异步请求
                        // var url="/menu/update";
                        axios.put("/menu/update.do",this.formData).then((response)=>{

                            this.dialogFormVisible4Edit=false;
                            if (response.data.flag) {
                                this.$message.success(response.data.message);
                                parent.location.href = "/pages/main.html?forward=menu.html";
                            } else {
                                this.$message.error(response.data.message);
                            }

                        }).catch((error) => {
                            this.showMessage(error)
                        }).finally(()=>{
                            // location.reload();
                            this.findPage();
                        })

                    } else {
                        //表单校验不通过
                    }
                });

            },
            //添加
            handleAdd () {
                //进行表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if(valid){
                        //表单校验通过，发生ajax请求，将录入的数据提交到后台进行处理

                        axios.post("/menu/add.do",this.formData).then((res) => {
                            //关闭新增窗口
                            this.dialogFormVisible = false;
                            if(res.data.flag){//执行成功
                                //新增成功后，重新调用分页查询方法，查询出最新的数据
                                this.findPage();
                                // location.reload();
                                //弹出提示信息
                                this.$message({
                                    message:res.data.message,
                                    type:'success'
                                });
                                // this.$message.success()
                            }else{//执行失败
                                //弹出提示
                                this.$message.error(res.data.message);
                            }
                        }).catch(error=> {
                            this.showMessage(error)
                        });
                    }else{
                        //校验不通过
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                        return false;
                    }
                });
            },
            // 弹出添加窗口
            handleCreate() {

                //弹出新增窗口
                this.dialogFormVisible = true;
                // axios.get("/menu/findParent.do").then(resp=>{
                //
                //     this.menuData =resp.data.data;
                // });

                this.resetForm();
            },
            // 弹出编辑窗口
            handleUpdate(row) {

                // var url="/menu/"+row.id;

                // axios.get("/menu/findById.do?id="+row.id).then(response=>{
                //     if (response.data.flag){
                //         //设置数据回显
                //         this.formData= response.data.data;
                //         //显示编辑框
                //         this.dialogFormVisible4Edit =true;
                //     }else {
                //         this.$message.error(response.data.message);
                //     }
                // });
                // axios.get("/menu/findParent.do").then(resp=>{
                //
                //     this.menuData =resp.data.data;
                // });
                this.formData = this.deepClone(row);
                //显示编辑框
                this.dialogFormVisible4Edit =true;

            },
            resetForm() {
                this.formData = {};//重置数据
            },
            //深拷贝
            deepClone(obj){
                return JSON.parse(JSON.stringify(obj));
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //设置最新的页码
                this.pagination.currentPage = currentPage;
                //重新调用findPage方法进行分页查询
                this.findPage();
            },
            // 删除
            handleDelete(row) {

                this.$confirm("您确定要删除此记录吗?","提示",{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    //用户点击确定 , 发送异步请求
                    // var url = '/menu/'+row.id ;
                    //发送异步请求
                    axios.delete("/menu/delete.do?id="+row.id).then(response=>{
                        if(response.data.flag){
                            this.$message.success(response.data.message);
                            parent.location.href = "/pages/main.html?forward=menu.html";
                            //重新调用findPage方法进行分页查询
                            this.findPage();
                        }else {
                            this.$message.error(response.data.message);
                        }
                    }).catch(error=> {
                        this.showMessage(error)
                    });
                })
            },
        }
    });


</script>
</html>